//
//  MyViewController.m
//  BaseFrame
//
//  Created by 孟学浩 on 2017/4/1.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "MyViewController.h"
#import "MyTableViewCell.h"
#import "AboutManufacturerViewController.h"
#import "AgentsViewController.h"
#import "OrderListViewController.h"
#import "BuildingViewController.h"
#import "JPUSHService.h"

#import "CollectViewController.h"
#import "DealViewController.h"
#import "SystemMsgViewController.h"
#import "SetupViewController.h"
#import "ComplainViewController.h"
#import "MyAddressViewController.h"
#import "MyReceiveAddressViewController.h"

@interface MyViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *headerView;
@property (nonatomic,strong) UIView *footerView;
@property (nonatomic,strong) UIImageView *imgView;
@property (nonatomic,strong) UILabel *userNameLabel;
@property (nonatomic,strong) UILabel *passwordLabel;

@property (nonatomic,strong) NSArray *dataArray;
@property (nonatomic,strong) NSDictionary *dataDictionary;
@property (nonatomic,strong) UIImage *image;

@end

//网络
@implementation MyViewController (NetWorking)

- (void)uploadImg{
    NSDictionary *parameters = @{@"auId":[UserModel sharedInstanced].userId};
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"register/updateauAvatars.action" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        NSData *data = UIImageJPEGRepresentation(self.image, .2);
        NSString *imgName = [NSString stringWithFormat:@"%ld.jpg",(NSUInteger)[NSDate date].timeIntervalSince1970];
        [formData appendPartWithFileData:data name:@"file" fileName:imgName mimeType:@"image/jpeg"];
        
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"ResponseStatus"] integerValue] == 0) {
            ShowMessage(responseObject[@"msg"]);
            [UserModel sharedInstanced].headImg = responseObject[@"selectByauId"];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

@end

//UI
@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的";
    [self.view addSubview:self.tableView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
}

#pragma mark - Event

- (void)setHeaderImgEvent{
    [self selectPhotoAlbumWithSelectPhotoHandle:^(UIImage *img) {
        _imgView.image = img;
        _image = img;
        [self uploadImg];
    }];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MyTableViewCell class])];
    cell.dataDictionary = self.dataArray[indexPath.section];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return !section?168:5;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = nil;
    if (!section) {
        view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 168)];
        view.backgroundColor = [UIColor whiteColor];
        
        _imgView = [UIImageView new];
        UIImageView *flagView = [UIImageView new];
        flagView.contentMode = UIViewContentModeScaleAspectFit;
        UIButton *changeImgBtn = [UIButton new];
        _userNameLabel = [UILabel new];
        _passwordLabel = [UILabel new];
        
        _imgView.backgroundColor = getColor(@"f1f2f6");
        _imgView.layer.masksToBounds = YES;
        _imgView.layer.cornerRadius = 5;
        [_imgView sd_setImageWithURL:[NSURL URLOffsetIPWithString:[UserModel sharedInstanced].headImg]];
        flagView.image = [UIImage imageNamed:@"pop"];
        [changeImgBtn addTarget:self action:@selector(setHeaderImgEvent) forControlEvents:UIControlEventTouchUpInside];
        _userNameLabel.font = DEF_FontSize_12;
        _passwordLabel.font = DEF_FontSize_12;
        _passwordLabel.textAlignment = NSTextAlignmentRight;
        _userNameLabel.textAlignment = NSTextAlignmentRight;
        
        _passwordLabel.text = [NSString stringWithFormat:@"%@****%@",[[UserModel sharedInstanced].mobile substringToIndex:3],[[UserModel sharedInstanced].mobile substringFromIndex:7]];
        _userNameLabel.text = [UserModel sharedInstanced].nameNick;
        
        UILabel *userNameTitleLabel = [UILabel new];
        UILabel *passwordTitleLabel = [UILabel new];
        userNameTitleLabel.text = @"用户名";
        passwordTitleLabel.text = @"手机号";
        userNameTitleLabel.font = DEF_FontSize_12;
        passwordTitleLabel.font = DEF_FontSize_12;
        
        CALayer *layer0 = [CALayer new];
        CALayer *layer1 = [CALayer new];
        CALayer *layer2 = [CALayer new];
        CALayer *layer3 = [CALayer new];
        layer0.backgroundColor = getColor(@"f1f2f6").CGColor;
        layer1.backgroundColor = getColor(@"f1f2f6").CGColor;
        layer2.backgroundColor = getColor(@"f1f2f6").CGColor;
        layer3.backgroundColor = getColor(@"f1f2f6").CGColor;
        
        _imgView.frame = CGRectMake(20, 15, 50, 50);
        flagView.frame = CGRectMake(0, 0, 13, 13);
        flagView.center = CGPointMake(SCREEN_WIDTH - 24, _imgView.center.y);
        layer0.frame = CGRectMake(0, 0, SCREEN_WIDTH, 5);
        layer1.frame = CGRectMake(0, CGRectGetMaxY(_imgView.frame)+10, SCREEN_WIDTH, .5);
        layer2.frame = CGRectMake(0, CGRectGetMaxY(layer1.frame)+44, SCREEN_WIDTH, .5);
        layer3.frame = CGRectMake(0, CGRectGetMaxY(view.frame)-5, SCREEN_WIDTH, 5);

        userNameTitleLabel.frame = CGRectMake(10, CGRectGetMaxY(layer1.frame), SCREEN_WIDTH-20, 44);
        passwordTitleLabel.frame = CGRectMake(10, CGRectGetMaxY(layer2.frame), SCREEN_WIDTH-20, 44);
        _userNameLabel.frame = CGRectMake(10, CGRectGetMaxY(layer1.frame), SCREEN_WIDTH-20, 44);
        _passwordLabel.frame = CGRectMake(10, CGRectGetMaxY(layer2.frame), SCREEN_WIDTH-20, 44);
        changeImgBtn.frame = CGRectMake(0, 5, SCREEN_WIDTH, 70);
        
        [view addSubview:_imgView];
        [view addSubview:_userNameLabel];
        [view addSubview:_passwordLabel];
        [view addSubview:flagView];
        [view addSubview:userNameTitleLabel];
        [view addSubview:passwordTitleLabel];
        [view.layer addSublayer:layer0];
        [view.layer addSublayer:layer1];
        [view.layer addSublayer:layer2];
        [view.layer addSublayer:layer3];
        [view addSubview:changeImgBtn];

    }
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 1:{
            CollectViewController *vc = [CollectViewController new];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }break;
        case 2:{
            DealViewController *vc = [DealViewController new];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }break;
        case 3:{
            SystemMsgViewController *vc = [SystemMsgViewController new];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }break;
        case 4:{
            SetupViewController *vc = [SetupViewController new];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }break;
        case 5:{
            MyAddressViewController *vc = [MyAddressViewController new];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }break;
        case 6:{
            ComplainViewController *vc = [ComplainViewController new];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }break;
        case 7:{
            MyReceiveAddressViewController *vc = [MyReceiveAddressViewController new];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }break;
        default:
            break;
    }
}

#pragma mark - set & get

- (UITableView *)tableView{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionHeaderHeight = .01;
        _tableView.sectionFooterHeight = 5;
        _tableView.tableFooterView = self.footerView;
        _tableView.tableHeaderView = self.headerView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.separatorInset = UIEdgeInsetsZero;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([MyTableViewCell class])];
        
    }
    return _tableView;
}

- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];

    }
    return _headerView;
}

- (UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    }
    return _footerView;
}

- (NSArray *)dataArray{
    if (!_dataArray) {
        _dataArray = @[@{@"img":@"my_sel1",@"title":@"个人信息",@"flag":@"0"},
                       @{@"img":@"shop",@"title":@"店铺收藏",@"flag":@"1"},
                       @{@"img":@"money",@"title":@"交易记录",@"flag":@"1"},
                       @{@"img":@"system",@"title":@"系统消息",@"flag":@"1"},
                       @{@"img":@"setup",@"title":@"我的设置",@"flag":@"1"},
                       @{@"img":@"location1",@"title":@"我的位置",@"flag":@"1"},
                       @{@"img":@"complain",@"title":@"我的投诉",@"flag":@"1"},
                       @{@"img":@"address",@"title":@"我的收货地址",@"flag":@"1"}];
    }
    return _dataArray;
}

- (NSDictionary *)dataDictionary{
    if (!_dataDictionary) {
        _dataDictionary = @{};
    }
    return _dataDictionary;
}

@end
